#!/usr/bin/env python
# Copyright 2010-2012 RethinkDB, all rights reserved.
import sys, os, time
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'common')))
import http_admin, driver, workload_runner, scenario_common
from vcoptparse import *

op = OptParser()
scenario_common.prepare_option_parser_mode_flags(op)
op["num-changes"] = IntFlag("--num-changes", 50)
opts = op.parse(sys.argv)

with driver.Metacluster() as metacluster:
    cluster = driver.Cluster(metacluster)
    executable_path, command_prefix, serve_options = scenario_common.parse_mode_flags(opts)
    print "Starting cluster..."
    processes = [driver.Process(cluster,
                                driver.Files(metacluster, db_path = "db-%d" % i, log_path = "create-output-%d" % i,
                                             executable_path = executable_path, command_prefix = command_prefix),
                                log_path = "serve-output-%d" % i,
        executable_path = executable_path, command_prefix = command_prefix, extra_options = serve_options)
        for i in xrange(2)]
    for process in processes:
        process.wait_until_started_up()

    print "Creating namespace..."
    http = http_admin.ClusterAccess([("localhost", p.http_port) for p in processes])
    dc1 = http.add_datacenter()
    dc2 = http.add_datacenter()
    http.move_server_to_datacenter(processes[0].files.machine_name, dc1)
    http.move_server_to_datacenter(processes[1].files.machine_name, dc2)

    primary_dc, secondary_dc = dc1, dc2
    ns = scenario_common.prepare_table_for_workload(opts, http, primary = primary_dc, affinities = {primary_dc: 0, secondary_dc: 1})
    http.wait_until_blueprint_satisfied(ns)

    for i in xrange(opts["num-changes"]):
        print "Swap %d..." % i
        primary_dc, secondary_dc = secondary_dc, primary_dc
        http.move_namespace_to_datacenter(ns, primary_dc)
        http.set_namespace_affinities(ns, {primary_dc: 0, secondary_dc: 1})
        http.wait_until_blueprint_satisfied(ns)
        cluster.check()
        http.check_no_issues()

    cluster.check_and_stop()

